#include "tablemodel.h"
#include "commonhelper.h"

TableModel::TableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    m_headList<<"商品编号"<<"全称"<<"简称"
            <<"包装数量"<<"单位"<<"种类"
            <<"售价"<<""<<"进价"<<"增加日期";
    QStringList dataList;
    dataList<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234"<<"1234";
    m_dataList.append(dataList);
    m_dataList.append(dataList);
    m_dataList.append(dataList);
    m_dataList.append(dataList);


    xdebug<<m_headList;
    xdebug<<m_dataList;
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    xdebug;
    // FIXME: Implement me!
    if(orientation==Qt::Vertical)
        //return QAbstractTableModel::headerData(section,orientation,role);
        return QVariant();
    if(role != Qt::DisplayRole)//添加此判断解决水平表头不能显示的问题
        return QVariant();
    QVariant value;
    if(section >= 0 && section < m_headList.size() && orientation == Qt::Horizontal)
        value = m_headList.at(section);
    //qdebug<<value;
    return value;
}

void TableModel::setData( const QList<QStringList> dataList)
{
    beginResetModel();
    m_dataList = dataList;
    endResetModel();
}

void TableModel::setHeaderData(const QStringList headList)
{
    beginResetModel();
    m_headList = headList;
    endResetModel();
}


int TableModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    // FIXME: Implement me!
}

int TableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    // FIXME: Implement me!
}

QVariant TableModel::data(const QModelIndex &index, int role) const
{
    xdebug;
    if (!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole) {
         int row = index.row();
         if(row > m_dataList.size())
             return QVariant();
         return m_dataList[row].at(index.column());
    }
    return QVariant();
}
